//
//  JBWHomeRetweetView.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/5.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit
import SnapKit

class JBWHomeRetweetView: UIView {

    //记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    //提供属性共外界赋值
    var statusViewModel: JBWStatusViewModel?{
        didSet{
            //赋值
            contentLabel.text = statusViewModel?.homeModel?.retweeted_status?.text
       
            //卸载约束
            retweetViewBottomConstraint?.deactivate()
            
            //如果有配图
            if let pictures = statusViewModel?.homeModel?.retweeted_status?.pic_urls,pictures.count  > 0 {
                //赋值
                pictureView.pictureList = pictures
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.isHidden = false
            } else {
                //无配图
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel.snp.bottom).offset(10).constraint
                })
                pictureView.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        contentLabel.snp.makeConstraints { (make) in
            
            make.left.top.equalTo(10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        //转发微博底部约束
        snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
        }
        
//        contentLabel.text = "这是转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博转发微博"
    }
    
    //懒加载控件
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.black, fontSize: JBWNormalFontSize, maxWeight: JBWSCREENW - 20)
    //配图
    private lazy var pictureView: JBWHomePictureView = {
        let view = JBWHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
